<template>
  <div>
    <div class="whole">
      <div class="nav" v-if="active_swipe && active_swipe.length > 0">
        <div class="rule" v-if="rule" @click="clickRule">
          <img src="https://img.wifenxiao.com/h5-wfx/images/rules.png"><span>规则</span>
        </div>
        <!-- 小程序端用swiper -->
        <wx-swiper class="my-swipe" :circular="true" indicator-color="#ebedf0" indicator-active-color="#F20B22" :autoplay="autoplay" :indicator-dots="showIndicators">
          <wx-swiper-item class="imgadv-wx-swiper-item" v-for="(item,index) in active_swipe" :key="index">
            <a :href="item.url? item.url: 'javascript:;'">
              <img :src="item.img" :show-menu-by-longpress="true" mode="aspectFill"/>
            </a>
          </wx-swiper-item>
        </wx-swiper>
      </div>
      <template v-if="activeName.length > 0">
        <active-nav :activeName="activeName" @activePage="activePage" :id="id"></active-nav>
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多数据了！"
          @load="onLoad"
        >
          <active-list  :finished="finished" :activeDetails="activeDetails" :describe="describe" />
        </van-list>
      </template>
      <template v-else-if="activeName.length == 0 && !finished">
				<no-data :noData="dataLoading"></no-data>
      </template>
      <template v-else>
				<no-data :noData="noData"></no-data>
      </template>
    </div>
    <van-popup
      v-model="ruleShow"
      closeable
      position="bottom"
      :style="{ height: '70%' }"
    >
    <div class="rule-content">
      <div class="title">活动规则</div>
      <div v-html="rule" class="content"></div>
    </div>
      
    </van-popup>
  </div>
</template>

<script>
import Vue from 'vue'
import { fissionActivitySet, fissionActivityList } from '@/api/item/activity'
import activeList from './components/activeList'
import activeNav from './components/activeNav'
import noData from '@/components/noData/index'
import { Popup, List } from 'vant'
Vue.use(List)
Vue.use(Popup)
export default {
  components: {
    activeList,
    noData,
    activeNav
  },
  data() {
    return {
      loading: true,
      finished: false,
      describe: '',
      activeName: [],
      activeDetails: [],
      active_swipe: [],
      rule: '',
      p: 1,
      id: '',
      noData: {
        img: 'https://img.wifenxiao.com/h5-wfx/images/no_recharge_log.png',
        text: '暂无活动'
      },
      dataLoading: {
        text: '拼命加载中...',
        marginTop: '60px'
      },
      ruleShow: false,
      showIndicators: true,
      autoplay: true,
    }
  },
  created() {
    this.init()
  },
  mounted() {
    // 小程序触底加载更多
    window.addEventListener('reachbottom', () => {
      // console.log('触底了')
      this.onLoad()
    })
  },
  methods: {
    init() {
      this.$loadingWrap.show()
      fissionActivitySet().then(res => {
        this.$loadingWrap.close()
        if (res.status == 1) {
          this.active_swipe = res.data.banner_list
          this.rule = res.data.rule
          this.activeName = res.data.fission_list
          this.p = 1
          this.id = res.data.fission_list[0].id
          this.describe = res.data.fission_list[0].activity_desc
          this.getList()
        } else {
          this.$Error(res.msg)
        }
      })
    },
    getList() {
      fissionActivityList({ id: this.id || this.activeName[0].id, p: this.p }).then(res => {
        this.$loadingWrap.close()
        if (res.status == 1) {
          if (this.p == 1) {
            this.activeDetails = res.data.list
          } else {
            this.activeDetails = this.activeDetails.concat(res.data.list)
          }
          // 加载状态结束
          this.loading = false
          if (res.data.list.length < 10) {
            this.finished = true
          } else {
            this.p++
            setTimeout(() => {
              this.getList()
            }, 1000)
          }
        } else {
          this.$Error(res.msg)
        }
      })
    },
    onLoad() {
      this.getList()
    },
    activePage(id, describe) {
      this.p = 1
      this.id = id
      // this.$loadingWrap.show()
      this.loading = true
      this.finished = false
      this.activeDetails = []
      this.getList()
      this.describe = describe
    },
    clickRule() {
      this.ruleShow = true
    }
  }
}
</script>

<style lang="scss">
  .whole {
      margin: 16px;
  }
  .nav {
    margin-bottom: 16px;
    // padding-bottom: 24px;
    position: relative;
    .rule {
      position: absolute;
      right: -16px;
      top: 16px;
      font-size: 24px;
      color: #fff;
      background: rgba(0, 0, 0, .3);
      border-top-left-radius:32px;
      border-bottom-left-radius:32px;
      z-index: 9;
      width: 116px;
      text-align: center;
      height: 48px;
      line-height: 48px;
      display: flex;
      align-items: center;
      justify-content: center;
      // .icon {
      //   font-size: 12px;
      // }
      img {
        width: 32px;
        height: 32px;
        margin-right: 4px;
      }
    }
    .my-swipe {
      height: 260px;
      border-radius: 12px;
      img {
        border-radius: 12px;
        width: 100%;
        height: 100%;
        object-fit: cover;
      }
    }
  }
  .rule-content {
    // padding: 20px;
    .title {
      font-size: 30px;
      font-weight: bold;
      text-align: center;
      border-bottom: 1px solid #eee;
      padding: 30px 0;
    }
    .content {
      padding: 40px 20px;
    }
  }
</style>